package com.cinnamon.rss.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Table(name="FEEDS")
public class Feed implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name="ID")
	private long id;
	
	@Column(name="TITLE")
	private String title;
	
	@Column(name="LINK")
	private String link;
	
	@Lob
	@Column(name="DESCRIPTION")
	private String description;
			
	@Column(name="PUB_DATE")
	private String pubDate;

	@Column(name="RSS_SOURCE_LINK")
	private String rssSourceLink;
	
	@OrderBy("pubDate DESC")
	@OneToMany(cascade=CascadeType.ALL, orphanRemoval=true, fetch=FetchType.EAGER)
	private List<Item> items = new ArrayList<Item>();
	
	@ManyToOne
	@JoinColumn(name="USERNAME")
	private User user;
	
	public Feed() {}
		
	public Feed(String title, String link, String description,
			String pubDate) {
	
		this.title = title;
		this.link = link;
		this.description = description;
		this.pubDate = pubDate;		
	}

	public Feed(String title, String link, String description,
			String pubDate, String rssSourceLink) {
	
		this.title = title;
		this.link = link;
		this.description = description;
		this.pubDate = pubDate;		
		this.rssSourceLink = rssSourceLink;
	}
	
	public long getId() {
		return id;	
	}
	
	public void setId(long id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getLink() {
		return link;
	}
	public void setLink(String link) {
		this.link = link;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getPubDate() {
		return pubDate;
	}
	public void setPubDate(String pubDate) {
		this.pubDate = pubDate;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public List<Item> getItems() {
		return items;
	}

	public void setItems(List<Item> items) {
		this.items = items;
	}	

	public String getRssSourceLink() {
		return rssSourceLink;
	}

	public void setRssSourceLink(String rssSourceLink) {
		this.rssSourceLink = rssSourceLink;
	}

	@Override
	public String toString() {
		return "Feed [id=" + id + ", title=" + title + ", link=" + link
				+ ", description=" + description + ", pubDate=" + pubDate;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((rssSourceLink == null) ? 0 : rssSourceLink.hashCode());
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Feed other = (Feed) obj;
		if (rssSourceLink == null) {
			if (other.rssSourceLink != null)
				return false;
		} else if (!rssSourceLink.equals(other.rssSourceLink))
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		return true;
	}
	
	
}
